'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, BOOLEAN, DATE, DECIMAL, ENUM } = app.Sequelize;

  const ShoppingList = app.model.define('shopping_list', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    item_name: {
      type: STRING(200),
      allowNull: false,
      comment: '商品名称',
    },
    category_id: {
      type: INTEGER,
      comment: '分类ID',
    },
    description: {
      type: TEXT,
      comment: '商品描述',
    },
    estimated_price: {
      type: DECIMAL(10, 2),
      comment: '预估价格',
    },
    priority: {
      type: ENUM('low', 'medium', 'high', 'urgent'),
      defaultValue: 'medium',
      comment: '优先级',
    },
    color_preference: {
      type: STRING(100),
      comment: '颜色偏好',
    },
    size_preference: {
      type: STRING(50),
      comment: '尺码偏好',
    },
    brand_preference: {
      type: STRING(100),
      comment: '品牌偏好',
    },
    occasion: {
      type: STRING(100),
      comment: '使用场合',
    },
    season: {
      type: ENUM('spring', 'summer', 'autumn', 'winter', 'all'),
      comment: '适用季节',
    },
    is_purchased: {
      type: BOOLEAN,
      defaultValue: false,
      comment: '是否已购买',
    },
    purchased_at: {
      type: DATE,
      comment: '购买时间',
    },
    actual_price: {
      type: DECIMAL(10, 2),
      comment: '实际价格',
    },
    store_name: {
      type: STRING(200),
      comment: '购买店铺',
    },
    notes: {
      type: TEXT,
      comment: '备注',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'shopping_lists',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '购物清单表',
  });

  // 关联关系
  ShoppingList.associate = function() {
    // 属于用户
    app.model.ShoppingList.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });

    // 关联分类
    app.model.ShoppingList.belongsTo(app.model.Category, {
      foreignKey: 'category_id',
      as: 'category',
    });
  };

  return ShoppingList;
};
